﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuestionsAnswers.UI.Models;
using QuestionsAnswers.UI.Models.QuestionsAnswers;


namespace QuestionsAnswers.UI.Controllers
{ 
    public class CommentController : Controller
    {
        private readonly QuestionsAnswersContext _db = new QuestionsAnswersContext();

        //
        // GET: /Comment/

        public ViewResult Index(int id)
        {
            var comments = _db.Comments.Include(c => c.User).Include(c => c.Answer).Where(c => c.AnswerId == id);
            ViewBag.Answer = _db.Answers.Find(id);
            return View(comments.ToList());
        }

        //
        // GET: /Comment/Details/5

        //public ViewResult Details(int id)
        //{
        //    Comment comment = _db.Comments.Find(id);
        //    return View(comment);
        //}

        //
        // GET: /Comment/Create
         [Authorize]
        public ActionResult Create(int id)
        {
            Answer answer = _db.Answers.Find(id);
            string user = User.Identity.Name;
            var userMem = _db.Users.FirstOrDefault(x => x.UserName == user);
             //Debug.Assert(userMem != null, "userMem != null");
             var comment = new Comment { AnswerId= answer.AnswerId, UserId = userMem.UserId, User = userMem, Answer = answer };
            return Request.IsAjaxRequest()?(ActionResult) PartialView(comment): View(comment);
        } 

        //
        // POST: /Comment/Create
         [Authorize]
        [HttpPost]
        public ActionResult Create(Comment comment)
        {
            if (ModelState.IsValid)
            {
                _db.Comments.Add(comment);
                _db.SaveChanges();
                return RedirectToAction("Index", new { id = comment.AnswerId });  
            }

            //ViewBag.UserId = new SelectList(_db.Users, "UserId", "UserName", comment.UserId);
            //ViewBag.AnswerId = new SelectList(_db.Answers, "AnswerId", "Text", comment.AnswerId);
            return View(comment);
        }
        
        //
        // GET: /Comment/Edit/5
 
        public ActionResult Edit(int id)
        {
            //Comment comment = _db.Comments.Find(id);
            //ViewBag.UserId = new SelectList(_db.Users, "UserId", "UserName", comment.UserId);
            //ViewBag.AnswerId = new SelectList(_db.Answers, "AnswerId", "Text", comment.AnswerId);
            Comment comment = _db.Comments.Include("User").Include("Answer").Where(x => x.CommentId == id).FirstOrDefault();

            return Request.IsAjaxRequest() ? (ActionResult)PartialView(comment) : View(comment);
        }

        //
        // POST: /Comment/Edit/5

        [HttpPost]
        public ActionResult Edit(Comment comment)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(comment).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index", new { id = comment.AnswerId });
            }
            //ViewBag.UserId = new SelectList(_db.Users, "UserId", "UserName", comment.UserId);
            //ViewBag.AnswerId = new SelectList(_db.Answers, "AnswerId", "Text", comment.AnswerId);
            return View(comment);
        }

        //
        // GET: /Comment/Delete/5
 
        public ActionResult Delete(int id)
        {
            Comment comment = _db.Comments.Find(id);
            return View(comment);
        }

        //
        // POST: /Comment/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            var comment = _db.Comments.Include(c => c.Answer).Include(c => c.User).First(c => c.CommentId == id);
            int questionId = comment.Answer.QuestionId;
     _db.Comments.Remove(comment);
            _db.SaveChanges();
            return RedirectToAction("Index", "Answer", new { id = questionId });
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}